package cn.deepxi.org.controller;

import cn.deepxi.base.query.BaseQuery;
import cn.deepxi.org.domain.Shop;
import cn.deepxi.org.service.IShopService;
import cn.deepxi.utils.AjaxResult;
import cn.deepxi.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = "部门接口",description = "描述接口的注解")
@Controller
@RequestMapping("/shop")
@ResponseBody
//@CrossOrigin
public class ShopController {
    @Autowired
    private IShopService shopService;
//    @RequestMapping(value="/saveOrUpt",method = RequestMethod.PUT)
    @PutMapping
    @ApiOperation(value = "部门修改或者添加",notes="有id就是修改，没有id就是新增")
    public AjaxResult saveOrUpt(@RequestBody Shop shop){
        try {
            if(shop.getId() !=null){
                shopService.update(shop);
            }else{
                shopService.save(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

//    @RequestMapping(value = "/del",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

//    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.loadById(id);

    }

//    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }
    @PostMapping
//    高级查询及分页的接口
    public QueryResult<Shop> queryResult(@RequestBody BaseQuery query){

        return shopService.queryPage(query);
    }


}
